package com.wen.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import java.io.IOException;
/*
  value="/*" 表示映射所有的路径，所有的请求都会被这个filter处理
  urlPatterns = {"/*"},
 */

@WebFilter(filterName = "EncodingFilter", value = "/*")
public class EncodingFilter implements Filter {
    //定义计数器

    private int hitCount;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //重置点击计数器
        hitCount = 0;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        //网站的总点击量
        hitCount++;
        System.out.println("网站访问统计：" + hitCount);
        //设置request和response的编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //
        chain.doFilter(request, response);
    }
}
